package com.ple.app.user;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.Toast;

import com.esri.android.map.GraphicsLayer;
import com.esri.android.map.MapOnTouchListener;
import com.esri.android.map.MapView;
import com.esri.android.map.ags.ArcGISFeatureLayer;
import com.esri.core.geometry.Geometry;
import com.esri.core.geometry.GeometryEngine;
import com.esri.core.geometry.Line;
import com.esri.core.geometry.Point;
import com.esri.core.geometry.Polygon;
import com.esri.core.geometry.Polyline;
import com.esri.core.map.CallbackListener;
import com.esri.core.map.FeatureEditResult;
import com.esri.core.map.Graphic;
import com.esri.core.symbol.SimpleFillSymbol;
import com.esri.core.symbol.SimpleLineSymbol;
import com.esri.core.symbol.SimpleMarkerSymbol;

import java.util.ArrayList;

/**
 * 用户绘制点，线，面；第二种交互方式。
 * 通过监听地图的单击事件进行多边形的绘制
 * Created by John on 2016/12/7.
 */

public class DrawGeometryHelper2 extends MapOnTouchListener {
    //判定当前绘制的geometry的类型
    private Geometry.Type type = null;
    // 绘制的起点
    private Point ptStart = null;
    // 前一个点
    private Point ptPrevious = null;
    // 所有的点
    private ArrayList<Point> points = null;
    // 临时的polygon
    private Polygon tempPolygon = null;
    // return polygon
    private Polygon polygon = null;
    //
    private MapView mapview;
    //GraphicLayer
    private GraphicsLayer graphicsLayer;
    //是否重置操作
    private boolean isChoose = true;
    //基本符号
    private SimpleLineSymbol lineSymbol;
    private SimpleFillSymbol fillSymbol;
    private SimpleMarkerSymbol markerSymbol;

    //构造函数
    public DrawGeometryHelper2(Context context, MapView view, GraphicsLayer g,Polygon p) {
        super(context, view);

        this.mapview = view;
        this.graphicsLayer = g;
        this.polygon= p;
        this.points = new ArrayList<Point>();
        this.initSymbol();
    }

    //是否重置操作
    public boolean isChoose() {
        return isChoose = true;
    }

    //初始化符号
    private void initSymbol() {
        lineSymbol = new SimpleLineSymbol(Color.BLUE, 2, SimpleLineSymbol.STYLE.DASH);
        markerSymbol = new SimpleMarkerSymbol(Color.RED, 15, SimpleMarkerSymbol.STYLE.CROSS);
        fillSymbol = new SimpleFillSymbol(Color.CYAN);
        fillSymbol.setAlpha(50);
    }

    //设定绘制的类型
    public void setType(String type) {
        if (type.equalsIgnoreCase("POINT")) {
            this.type = Geometry.Type.POINT;
        } else if (type.equalsIgnoreCase("POLYLINE")) {
            this.type = Geometry.Type.POLYLINE;
        } else {
            this.type = Geometry.Type.POLYGON;
        }
    }

    //返回绘制的类型
    public Geometry.Type getType() {
        return type;
    }

    public Polygon getPolygon() {
        return polygon;
    }

    //监听事件
    @Override
    public boolean onSingleTap(MotionEvent point) {
        if(isChoose){
            ptStart = null;
            ptPrevious= null;
            tempPolygon = null;
            points.clear();
        }
        //记录当前点
        Point ptCurrent = mapview.toMapPoint(new Point(point.getX(), point.getY()));

        if (ptStart == null) graphicsLayer.removeAll();
        if (type == Geometry.Type.POINT) {
            ptStart = ptCurrent;
            Graphic g = new Graphic(ptStart, markerSymbol);
            graphicsLayer.addGraphic(g);
        } else {//绘多线段和多边形
            points.add(ptCurrent);//将当前点加入到点集合中

            if (ptStart == null) {//如果第一个点为空，将当前点设置为第一个点
                ptStart = ptCurrent;
                Graphic g = new Graphic(ptStart, new SimpleMarkerSymbol(Color.CYAN,5, SimpleMarkerSymbol.STYLE.CROSS));
                graphicsLayer.addGraphic(g);
            } else {
                //先绘制第一个点
                Graphic g = new Graphic(ptCurrent, new SimpleMarkerSymbol(Color.CYAN,5, SimpleMarkerSymbol.STYLE.CROSS));
                graphicsLayer.addGraphic(g);

                //生成当前线段，由前一个和当前点组成
                Line line = new Line();
                line.setStart(ptPrevious);
                line.setEnd(ptCurrent);

                if (type == Geometry.Type.POLYLINE) {
                    //绘制当前线段
                    Polyline polyline = new Polyline();
                    polyline.addSegment(line, true);

                    Graphic graphicLine = new Graphic(polyline, lineSymbol);
                    graphicsLayer.addGraphic(graphicLine);

                } else {
                    if (tempPolygon == null){
                        tempPolygon = new Polygon();
                    }
                    tempPolygon.addSegment(line, false);

                    graphicsLayer.removeAll();
                    Graphic graphicArea = new Graphic(tempPolygon, fillSymbol);
                    graphicsLayer.addGraphic(graphicArea);

                    polygon = tempPolygon;
                }
            }
            ptPrevious = ptCurrent;
            isChoose = false;
            return true;
        }
        return super.onSingleTap(point);
    }

}
